/**
 * @author Liu Cong
 */
(function(){
const nsIFilePicker = Components.interfaces.nsIFilePicker;

var fp = Components.classes["@mozilla.org/filepicker;1"]
	           .createInstance(nsIFilePicker);
fp.init(window, "Open a bin file", nsIFilePicker.modeOpen);
fp.appendFilters(nsIFilePicker.filterAll);

var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
	var file = fp.file;
} else {
	return;
}

var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
                        createInstance(Components.interfaces.nsIFileInputStream);
fstream.init(file, -1, 0, 0);

var request = new FireAMF.Request();
request.initialize(fstream);
fstream.close();

var headers = request.getAmfHeaders();
var bodies = request.getAmfBodies();
var bs = ['TargetURI: '+b.targetUri+'\nResponseURI: '+b.responseUri+'\nData: '+((b.data instanceof FireAMF.Message)?b.data:dumpobj(b.data)) for each(b in bodies)];
Application.console.log(dumpobj(headers));
Application.console.log(bs);

return;

function dumpobj(obj){
    switch(typeof obj){
        case 'object':
            if(obj == null) return obj;
			if(obj instanceof Array){
                return '[' + [dumpobj(p) for each(p in obj)].join(', ') + ']';
            } else {
                return '{\n' + [dumpobj(p)+': '+dumpobj(obj[p]) for(p in obj)].join(',\n') + '\n}';
            }
            break;
        case 'function':
            //return obj.toSource();
			return '[function]';
            break;
        case 'string':
            return '"' + obj.replace(/"/g, '\\"') + '"';
            break;
        case 'undefined':
        case 'boolean':
        case 'number':
        default:
            return obj;
            
    }
}
})();
